package com.gitee.yannzh.rune.extension.mp.query;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.gitee.yannzh.rune.core.base.pagination.PageOption;
import com.gitee.yannzh.rune.core.base.pojo.Param;
import com.gitee.yannzh.rune.core.exception.SystemException;
import com.gitee.yannzh.rune.core.util.SqlUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yann
 * @version 1.0
 * @description query对象基类
 * @date 2022/2/16 17:19
 */
@Getter
@Setter
@ToString
public abstract class BaseQuery implements Param {

    @Min(value = 1, message = "页码最小值为1")
    private Long pageNo;

    @Min(value = 1, message = "每页条数最小值为1")
    private Long pageSize;


    private String orderBy;

    private String isAsc;


    /**
     * 当前记录起始索引 默认值
     */
    public static final long DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数 默认值 默认查全部
     */
    public static final long DEFAULT_PAGE_SIZE = 20L;


    /**
     * 生成分页对象
     */
    //TODO 校验排序字段是否存在
    @Override
    public PageOption ofPage() {
        long num = ObjectUtil.defaultIfNull(pageNo, DEFAULT_PAGE_NUM);
        long size = ObjectUtil.defaultIfNull(pageSize, DEFAULT_PAGE_SIZE);
        if (num <= 0) {
            num = DEFAULT_PAGE_NUM;
        }
        //        Page<T> page = Page.of(num, size);
//        List<OrderItem> orderItems = buildOrderItem();
//        if (CollUtil.isNotEmpty(orderItems)) {
//            page.addOrder(orderItems);
//        }
        return PageOption.builder().pageSize(size).pageNo(num).build();
    }

    private List<OrderItem> buildOrderItem() {
        if (StringUtils.isBlank(orderBy)) {
            return null;
        }
        String column = SqlUtil.escapeOrderBySql(orderBy);
        column = StrUtil.toUnderlineCase(column);

        // 兼容前端排序类型
        String[] orderByArr = column.split(",");
        String[] isAscArr = StringUtils.isNotBlank(isAsc) ? isAsc.split(",") : new String[]{"asc"};
//        isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});

        if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
            throw new SystemException("排序参数有误");
        }

        List<OrderItem> list = new ArrayList<>();
        // 每个字段各自排序
        for (int i = 0; i < orderByArr.length; i++) {
            String orderByStr = orderByArr[i];
            String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i];
            if ("asc".equals(isAscStr)) {
                list.add(OrderItem.asc(orderByStr));
            } else if ("desc".equals(isAscStr)) {
                list.add(OrderItem.desc(orderByStr));
            } else {
                throw new SystemException("排序参数有误");
            }
        }
        return list;
    }
}
